﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XsClient.Common;

namespace XsClient.GUI
{
    /// <summary>
    /// WindowSetting.xaml 的交互逻辑
    /// </summary>
    public partial class WindowSetting : Panuon.UI.Silver.WindowX
    {
        public WindowSetting()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            try {
                var sources= XsConfig.Current.Sources;
                int selectId = 0;
                int i = 0;
                foreach(var item in sources)
                {
                    cmbSource.Items.Add(item.Name + ":"  +item.BaseUrl);
                    panelSource.Items.Add(item.Name + ":" + item.BaseUrl);
                    if (item.ID == XsConfig.Current.DefaultID)
                    {
                        selectId =i;
                    }
                    i++;
                }
                cmbSource.SelectedIndex = selectId;
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            int id= cmbSource.SelectedIndex;
            if (id >= 0&&id< XsConfig.Current.Sources.Count)
            {
                var item= XsConfig.Current.Sources[id];
                XsConfig.Current.DefaultID = item.ID;
            }
            XsConfig.Current.Save();
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
        XsSource selectSource = null;
        private void panelSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try {
                selectSource = null;
                var sources = XsConfig.Current.Sources;
                int index= panelSource.SelectedIndex;
                if (index >= 0 && index < sources.Count)
                {
                    selectSource = sources[index];
                    txtXSName.Text = selectSource.Name;
                    txtSrcUrl.Text = selectSource.SrcUrl;
                    txtBaseUrl.Text = selectSource.BaseUrl;
                }

            } catch { }
        }

        private void txtBaseUrl_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (selectSource != null)
                {
                    selectSource.BaseUrl = txtBaseUrl.Text.Trim();
                }
            }
            catch { }

        }
    }
}
